//
//  NewsViewController.m
//  MtimeMovie
//
//  Created by lllyyy on 15/7/17.
//  Copyright (c) 2015年 lllyyy. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsModal.h"
#import "DataService.h"
#import "NewsCell.h"
#import "Header.h"
#import "UIViewExt.h"
#import "UIImageView+WebCache.h"
#import "newsImageListController.h"
static NSString *newsCellId = @"newsCellId";

@interface NewsViewController ()
    {
        UIImageView *_headImageView;
        UILabel *_headLabel;
    }

@end

@implementation NewsViewController

- (void)viewDidLoad {
    [self _createNewsTableView];
    [self _createHeaderView];
    [self _loadData];
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){
        self.title = @"新闻";
    }
    return self;
}

- (void)_createNewsTableView{
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    UINib *nib = [UINib nibWithNibName:@"NewsCell" bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:newsCellId];
}
- (void)_createHeaderView{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 200)];
    headerView.backgroundColor = [UIColor clearColor];
    _tableView.tableHeaderView = headerView;
    
    _headLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, kNavHeight, KWidth, 30)];
    _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KWidth, 200)];
    _headLabel.bottom = _headImageView.bottom;
    //_headImageView.image = [UIImage imageNamed:@"1"];
    _headLabel.textColor = [UIColor whiteColor];
    _headLabel.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:_headImageView];
    [self.view addSubview:_headLabel];
}


- (void)_loadData{
    _newsArray = [[NSMutableArray alloc]init];
    NSArray *newsArray= [DataService getJsonDataFromFile:@"news_list.json"];
    for (NSDictionary *dic in newsArray) {
        NewsModal *modal = [[NewsModal alloc]init];
        [modal setValuesForKeysWithDictionary:dic];
        [_newsArray addObject:modal];
}
    NewsModal *modal = _newsArray[0];
    
    _headLabel.text = modal.title;
    
    NSURL *url = [NSURL URLWithString:modal.image];
    
    [_headImageView sd_setImageWithURL:url];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _newsArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:newsCellId forIndexPath:indexPath];
    cell.news = _newsArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    newsImageListController *vc = [[newsImageListController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offY = scrollView.contentOffset.y;
    if (offY > 0) {
        _headImageView.top = -offY;
        _headLabel.bottom = _headImageView.bottom;
    }else{
        CGFloat newHeight = ABS(offY)-0+200;
        CGFloat newWidth = KWidth/200 *newHeight;
        _headImageView.frame = CGRectMake((KWidth-newWidth)/2, 0, newWidth, newHeight);
        _headLabel.bottom = _headImageView.bottom;
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end

